<template>
  <div class="test">
    <div
      class="bg"
      :style="{ 'background-image': `url(/images/${bgIndex}.jpg)` }"
    ></div>
    <div
      class="button"
      @click="isShow = !isShow"
    ></div>
    <div
      class="web"
      :style="{ top: isShow ? 0 : '-127px' }"
    >
      <ul id="nav-list">
        <li
          v-for="(item, i) in list"
          :data-title="item.title"
          :key="item.id"
          @click="handleClick(i)"
          @mouseover="handleMouseover(i)"
          @mouseout="handleMouseout"
        >
          <span :class="{ tips: active === i }"></span>
          <img :src="item.src" />
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isShow: false,
      active: 0,
      bgIndex: 0,
      list: [
        {
          id: 0,
          src: '/images/0.jpg',
          title: '你'
        },
        {
          id: 1,
          src: '/images/1.jpg',
          title: '是'
        },
        {
          id: 2,
          src: '/images/2.jpg',
          title: '最'
        },
        {
          id: 3,
          src: '/images/3.jpg',
          title: '棒'
        },
        {
          id: 4,
          src: '/images/4.jpg',
          title: '的'
        },
        {
          id: 5,
          src: '/images/5.jpg',
          title: '!'
        }
      ]
    }
  },
  methods: {
    handleClick(index) {
      // 选中效果,选中的同时已经记录了选择那种图片的索引
      this.active = index
    },
    handleMouseover(index) {
      this.bgIndex = index
    },
    handleMouseout() {
      this.bgIndex = this.active
    }
  }
}
</script>
<style lang="scss">
* {
  margin: 0;
  padding: 0;
}

.bg {
  position: absolute;
  top: 0;
  left: 0;
  z-index: -1;
  width: 100%;
  height: 100vh;
  background-image: url('/images/0.jpg');
  background-attachment: fixed;
  background-position: -50px 0;
  background-size: 120% 100%;
}

ul,
ol,
li {
  list-style: none;
}

.web {
  position: fixed;
  top: -127px;
  left: 0;
  width: 100%;
  height: 127px;
  line-height: 127px;
  text-align: center;
  /* border: 1px solid #ddd; */
  background: rgba(255, 255, 255, 0.86);
  transition: all 1s ease;
}

.web ul {
  width: 928px;
  overflow: hidden;
  display: inline-block;
  vertical-align: middle;
}

.web ul li {
  position: relative;
  width: 138px;
  height: 76px;
  line-height: 76px;
  float: left;
  margin-right: 20px;
  &:hover:after {
    content: attr(data-title);
    position: absolute;
    top: 0;
    left: 0;
    z-index: 520;
    background-color: rgba(0, 0, 0, 0.5);
    width: 100%;
    height: 100%;
    color: #fff;
  }
}

.web ul li:last-child {
  margin-right: 0;
}

.web ul li img {
  width: 138px;
  height: 76px;
}

.tips {
  width: 30px;
  height: 30px;
  display: block;
  position: absolute;
  top: 0;
  left: 0;
  z-index: 1314;
  background: url('/images/tips.png') -94px -4px;
}

.button {
  width: 36px;
  height: 56px;
  position: absolute;
  top: 0;
  left: 20px;
  background: url('/images/button.png');
  z-index: 10;
}
</style>
